package com.winz.cultureapp.widge;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.flyco.tablayout.medol.TabEntity;
import com.winz.cultureapp.R;

import java.util.ArrayList;


/**
 * 选择自愿服务频率
 */
public class SelVolunteerFrequencyDialog {

    private AlertDialog dialog;
    private final Button btn_ok;
    private final Window window;

    private String[] frequencys = {"每周一次以上", "每月一次以上"};
    private final CommonTabLayout tl_sex;

    private int[] mIconUnselectIds = {
            R.drawable.icon_cb_nor, R.drawable.icon_cb_nor};
    private int[] mIconSelectIds = {
            R.drawable.icon_cb_sel, R.drawable.icon_cb_sel};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private String frequency = frequencys[0];
    private final TextView tv_title;

    public SelVolunteerFrequencyDialog(final Context context){
        dialog = new AlertDialog.Builder(context).create();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setView(LayoutInflater.from(context).inflate(R.layout.dialog_sel_sex,null));
        dialog.show();

//        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
//        lp.width = (AppUtils.getScreenWidth((Activity) context)); //设置宽度
//        dialog.getWindow().setAttributes(lp);
        window = dialog.getWindow();
        window.setGravity(Gravity.CENTER); // 非常重要：设置对话框弹出的位置
        window.setContentView(R.layout.dialog_sel_sex);

        btn_ok = (Button) window.findViewById(R.id.btn_ok);
        tv_title = window.findViewById(R.id.tv_title);
        tl_sex = window.findViewById(R.id.tl_sex);

        for (int i = 0; i < frequencys.length; i++) {
            mTabEntities.add(new TabEntity(frequencys[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        tl_sex.setTabData(mTabEntities);
        tl_sex.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                frequency = frequencys[position];
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        dialog.dismiss();
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        dialog.dismiss();
    }

    public void setTv_title(String title){
        tv_title.setText(title);
    }


    public void setOkButtonOnClickListener(View.OnClickListener Listener) {
        btn_ok.setOnClickListener(Listener);
    }

    public String getSelFrequency() {
        return frequency;
    }

    public void show(){
        dialog.show();
    }

}
